import sys, os, subprocess

python_config = '/usr/bin/python2.6-config'
#python_config = '/usr/local/bin/python2.6-config'

x = subprocess.Popen(python_config + ' --includes', stdout=subprocess.PIPE, shell=True)
python_includes = x.stdout.read().strip().split()

x = subprocess.Popen(python_config + ' --libs', stdout=subprocess.PIPE, shell=True)
python_libs = x.stdout.read().strip().split()

x = subprocess.Popen(python_config + ' --ldflags', stdout=subprocess.PIPE, shell=True)
python_ldflags = x.stdout.read()

#os.system('swig -python -o memlink_client_python.c ../c/memlink_client.i')

#cflags   = '-ggdb -DRANGE_MASK_STR'
cflags   = '-ggdb -O2'
includes = ['.', '../../', '../c/'] + python_includes
#ldflags  = python_ldflags
ldflags  = ''
libpath  = ['.']
libs	 = python_libs + ['c', 'm', 'pthread']
files    = ['../../zzmalloc.c', '../../serial.c', '../../logfile.c', '../../utils.c', 
			'../../util/net.c','../c/memlink_client.c']

env = Environment(CCFLAGS=cflags, CPPPATH=includes, LINKFLAGS=ldflags, LIBPATH=libpath, LIBS=libs)
shared_files = []
for fn in files:
    name = os.path.basename(fn)[:-2]
    shared_files.append(env.SharedObject('client-' + name, fn))
shared_files.append('memlink_client_python.c')

env.SharedLibrary("memlink", shared_files)

if sys.platform.startswith('darwin'):
    Command('_cmemlink.so', 'libmemlink.dylib', [Move('$TARGET', '$SOURCE')])
else:
    Command('_cmemlink.so', 'libmemlink.so', [Move('$TARGET', '$SOURCE')])

